﻿
using System.Windows;
using System.Windows.Controls;
using WebSilverlight.Web;
using System.Collections.Generic;


namespace WebSilverlight.Views
{
    public enum GenderEnum { Hombre = 1, Mujer = 2 };
    public class EnumComboHelper
    {
        public GenderEnum Value{set; get;}
        public short Value1{ set; get; }      
        public string Description{set; get;}

        public List<EnumComboHelper> Items 
        {
            get
            {
                List<EnumComboHelper>  genList = new List<EnumComboHelper>();

                //genList.Add(new EnumComboHelper() { Value = GenderEnum.Hombre, Description = GenderEnumMetaData.Get });
                genList.Add(new EnumComboHelper() { Value = GenderEnum.Hombre, Value1 = 1, Description = "Hombre" });
                genList.Add(new EnumComboHelper() { Value = GenderEnum.Mujer, Value1 = 2, Description = "Mujer" });
                return genList;
            }
        }

    }
	public partial class ClienteEdit : StackPanel
	{
	#region Properties
				
		private ClienteEditFlip parentWindow;        
		public bool DialogResult { get; set; }
		public bool IsReadOnly{ 
			get
			{
				return this.dataFormCliente1.IsReadOnly;
			}set
			{
				this.EditButton.Visibility = System.Windows.Visibility.Collapsed;
				if(value)                
					this.EditButton.Visibility = System.Windows.Visibility.Visible;
				this.dataFormCliente1.IsReadOnly = value;
			}
		}
		private ClienteWrapper _Current;
		public ClienteWrapper Current
		{
			set {
				_Current = value;
                if (_Current != null) this.dataFormCliente1.CurrentItem = value;
			}
			get { return _Current; }
		}
        public bool BottomActionsVisible
        {
            set
            {
                if (value) this.BottomActions.Visibility = System.Windows.Visibility.Visible;
                else this.BottomActions.Visibility = System.Windows.Visibility.Collapsed;                    
            }
            get
            {                
                return false;
            }

        }
        public static List<EnumComboHelper> genList { get; set; }
    #endregion

		public ClienteEdit()
		{
			InitializeComponent();
			DialogResult = false;
			IsReadOnly = true;

            genList = new List<EnumComboHelper>();

            //genList.Add(new EnumComboHelper() { Value = GenderEnum.Hombre, Description = GenderEnumMetaData.Get });
            genList.Add(new EnumComboHelper() { Value = GenderEnum.Hombre, Description = "Hombre" });
            genList.Add(new EnumComboHelper() { Value = GenderEnum.Mujer, Description = "Mujer" });
                   
            


		}
		/// <summary>
		/// Ok Button Clicked
		/// TODO: Hacer lógica de NAvegación
		/// </summary>  
		private void OKButton_Click(object sender, RoutedEventArgs e)
		{
			if (dataFormCliente1.ValidateItem())
			{                
				DialogResult = true;                           
				dataFormCliente1.CommitEdit(true);                                
								
				if (IsReadOnly) this.parentWindow.NavigateToEnd();
				else
				{
					if (parentWindow.IsNew)
					{   
						this.parentWindow.DomainContext.ClienteWrappers.Add(Current);
						this.parentWindow.DomainContext.SubmitChanges();
						this.parentWindow.IsNew = false;                        
						this.parentWindow.DDS.Load();
					}
					if (Current.HasChanges)
						this.parentWindow.DDS.SubmitChanges();
					
					if (this.parentWindow.IsEditingFirstState) 
						this.parentWindow.NavigateToEnd();
					else
						this.parentWindow.NavigateToView();
				}
			}
		}
		/// <summary>
		/// Cancel Button Clicked
		/// </summary>   
		private void CancelButton_Click(object sender, RoutedEventArgs e)
		{
			this.DialogResult = false;
			this.dataFormCliente1.CancelEdit();
			this.parentWindow.DDS.RejectChanges();            
			
			if (IsReadOnly) this.parentWindow.NavigateToEnd();
			else if (this.parentWindow.IsEditingFirstState) this.parentWindow.NavigateToEnd();
			else this.parentWindow.NavigateToView();
		}
		/// <summary>
		/// Edit Button Clicked
		/// </summary>   
		private void EditButton_Click(object sender, RoutedEventArgs e)
		{
			this.parentWindow.NavigateToEdit();
		}
		/// <summary>
		/// Sets the Parent Window
		/// </summary>   
		/// <param name="window">The window to use as the parent.</param>
		public void SetParentWindow(ClienteEditFlip window)
		{
			this.parentWindow = window;
			Current = parentWindow.Current;              
		}
		/// <summary>
		/// Sets the initial value in the ComboBox.
		/// </summary>   
		private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if(e.AddedItems.Count != 0)
			{
				PaisWrapper pais = (PaisWrapper)e.AddedItems[0];                
				this.Current.PaisNombre = pais.PaisNombre;                    
			}
		}
		/// <summary>
		/// Sets the initial value in the ComboBox.
		/// Walkaround for binding the initial value.
		/// </summary>        
		private void ddsPais1_LoadedData(object sender, LoadedDataEventArgs e)
		{
			var component = this.dataFormCliente1.FindNameInContent("paisCombo");
			if (component != null)
			{                
				ComboBox combo = (ComboBox)component;
				combo.SelectedValue = Current.PaisId;
			}
		}        
	}
}
